import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/login/login.vue";

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) {
		return originalPush.call(this, location, onResolve, onReject)
	}
	return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter);

export const constantRoutes = [];
let rootLayout;
const createRouter = () => {
	// 获取当前登录人的shiroKey集合
	let tRoleLists = sessionStorage.getItem("tRoleLists");
	if (tRoleLists) {
		tRoleLists = JSON.parse(tRoleLists);
	} else {
		tRoleLists = [];
	}

	// 获取路由数据
	let routeData = sessionStorage.getItem("routeData");
	if (routeData) {
		routeData = JSON.parse(routeData);
	} else if (!routeData) {
		routeData = [];
	}

	constantRoutes.splice(0, constantRoutes.length);

	constantRoutes.push({
		path: "/",
		component: Login,
		meta: {
			requireAuth: false
		}
	});

	constantRoutes.push({
		path: "/login",
		component: Login,
		meta: {
			requireAuth: false
		}
	});

	rootLayout = {
		path: "/Layout",
		name: "Layout",
		component: () => import("@/layout/Layout.vue"),
		meta: {
			// 判断是否需要登录
			requireAuth: false,
		},
		children: [
			{
				path: "/home",
				name: "Home",
				component: () => import("@/views/home/home.vue"),
				icon:'el-icon-platform-eleme',
				meta: { title: "首页" }
			}
		]
	};

	constantRoutes.push(rootLayout);
	if (tRoleLists && tRoleLists.length > 0) {
		addDynamicRoutes(tRoleLists, routeData, rootLayout.children, null);
	}

	const router = new VueRouter({
		scrollBehavior: () => ({
			y: 1
		}),
		routes: constantRoutes,
		// base: "/admin/"
	});

	// 未登录拦截
	router.beforeEach((to, from, next) => {
		if (to.matched.some(record => record.meta.requireAuth)) {
			const token = localStorage.getItem("z-admin-token");
			if (token) {
				next();
			} else {
				next({
					path: '/',
					query: {
						redirect: to.fullPath
					}
				})
			}
		} else {
			next();
		}
	});

	return router;
}

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
	const newRouter = createRouter();
	router.matcher = newRouter.matcher; // reset router
}

/**
 * 添加动态菜单路由
 * @param tRoleLists 角色列表
 * @param roleRouteMapping 对应链接
 * @param layout 框架
 * @param basePath
 */
function addDynamicRoutes(tRoleLists = [], roleRouteMapping, layout, basePath) {
	roleRouteMapping.forEach(roleRouteItem => {
		tRoleLists.forEach(role => {
			let component = null;
			if (roleRouteItem.fileLocation) {
				if (roleRouteItem.fileLocation === 'ParentView') {
					component = () => import("../layout/ParentView.vue");
				} else {
					component = () => import("@/views/" + roleRouteItem.fileLocation + ".vue")
				}
			}

			const rootRouter = {
				path: null,
				name: null,
				component: component,
				query: null,
				meta: {
					icon: null,
					title: null,
					basePath: "",
					uuid: null,
					file: null,
				}
			};
			const l1Children = [];
			if (role === roleRouteItem.shiroKey) {
				rootRouter.path = roleRouteItem.path;
				rootRouter.name = roleRouteItem.title;
				rootRouter.meta.icon = roleRouteItem.icon;
				rootRouter.meta.title = roleRouteItem.metaTitle;
				rootRouter.meta.basePath = basePath;
				rootRouter.meta.uuid = roleRouteItem.uuid;
				rootRouter.meta.file = roleRouteItem.fileLocation;
				rootRouter.children = l1Children;

				rootRouter.props = route => ({
					id: route.query.id
				});

				if (!basePath && rootRouter.path.indexOf("/") === -1) {
					rootRouter.path = `/${rootRouter.path}`;
				}
				if (roleRouteItem.children && roleRouteItem.children.length > 0) {
					let basePathChild = "";
					let nextChildren = rootLayout.children;
					if (roleRouteItem.fileLocation === "ParentView") {
						basePathChild = roleRouteItem.path;
						nextChildren = l1Children;
					}
					addDynamicRoutes(
						tRoleLists,
						roleRouteItem.children,
						nextChildren,
						basePathChild
					);
				}
				layout.push(rootRouter);
			}
		});
	});
}

export default router;
